﻿using Microsoft.EntityFrameworkCore;
using TMS_Master.Entities;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.IdentityServer.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;

namespace TMS_Master.EntityFrameworkCore
{
    [ReplaceDbContext(typeof(IIdentityDbContext))]
    [ReplaceDbContext(typeof(ITenantManagementDbContext))]
    [ConnectionStringName("Default")]
    public class TMS_MasterDbContext : 
        AbpDbContext<TMS_MasterDbContext>,
        IIdentityDbContext,
        ITenantManagementDbContext
    {
        /* Add DbSet properties for your Aggregate Roots / Entities here. */
        
        #region Entities from the modules
        
        /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
         * and replaced them for this DbContext. This allows you to perform JOIN
         * queries for the entities of these modules over the repositories easily. You
         * typically don't need that for other modules. But, if you need, you can
         * implement the DbContext interface of the needed module and use ReplaceDbContext
         * attribute just like IIdentityDbContext and ITenantManagementDbContext.
         *
         * More info: Replacing a DbContext of a module ensures that the related module
         * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
         */
        
        //Identity
        public DbSet<IdentityUser> Users { get; set; }
        public DbSet<IdentityRole> Roles { get; set; }
        public DbSet<IdentityClaimType> ClaimTypes { get; set; }
        public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
        public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
        public DbSet<IdentityLinkUser> LinkUsers { get; set; }
        
        // Tenant Management
        public DbSet<Tenant> Tenants { get; set; }
        public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }

        #endregion
        
        public TMS_MasterDbContext(DbContextOptions<TMS_MasterDbContext> options)
            : base(options)
        {

        }

        #region 迁移

        public DbSet<UserInfo> UserInfos { get; set; }   //登录
        public DbSet<Address> Addr { get; set; }   //地址表
        public DbSet<Texture> Textures { get; set; }   //材质分类表
        public DbSet<Cargo_Information> Cargo_Informations { get; set; }   //物资采购表
        public DbSet<Collect_Fee> Collect_Fees { get; set; }   //收费用户表
        public DbSet<Goods_Receiving> Goods_Receivings { get; set; }   //物资领用表
        public DbSet<Materials_Company> Materials_Companys { get; set; }   //物资
        public DbSet<Paying_User> Paying_Users { get; set; }   //付费用户表
        public DbSet<TmsType> TmsTypes { get; set; }   //类型表
        public DbSet<Statistical_Goods> Statistical_Good { get; set; }   //物资统计表
        public DbSet<TodoItem> TodoItems { get; set; }

        #endregion

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            /* Include modules to your migration db context */

            builder.ConfigurePermissionManagement();
            builder.ConfigureSettingManagement();
            builder.ConfigureBackgroundJobs();
            builder.ConfigureAuditLogging();
            builder.ConfigureIdentity();
            builder.ConfigureIdentityServer();
            builder.ConfigureFeatureManagement();
            builder.ConfigureTenantManagement();

            /* Configure your own tables/entities inside here */

            //builder.Entity<YourEntity>(b =>
            //{
            //    b.ToTable(TMS_MasterConsts.DbTablePrefix + "YourEntities", TMS_MasterConsts.DbSchema);
            //    b.ConfigureByConvention(); //auto configure for the base class props
            //    //...
            //});

            //配置实体模型到数据模型的一些规则
            builder.Entity<Cargo_Information>(b =>
            {
                b.ToTable("Cargo_Information"); //表名
                b.Property("CargoName").HasMaxLength(200) //长度
                                    .HasColumnName("CargoName") //数据库字段名称
                                    .HasColumnType("varchar") //数据库字段类型
                                    .IsRequired(); //非空 
            });
        }
    }
}
